'use strict'
class note {
    constructor() {
        this.param = '/:noteId'
    }
    __mid(){// 启用token-verify中间件
        return [
          {
            name:'token-verify',
            path:['list','get','post','delete']
          }
        ]
    }
    async list(c){ //请求用户笔记列表
        let userId = c.box.user.userId;
        let sqltext = `SELECT * FROM note WHERE "userId"=$1 ORDER BY "noteId" DESC`;
        let r = await c.service.psql.query(sqltext,[userId]);
        console.log(r.rows);
        c.send(JSON.stringify(r.rows));
    }
    async get(c){ //请求用户某一笔记
        let userId = c.box.user.userId;
        let sqltext = `SELECT * FROM note WHERE "userId"=$1 AND "noteId"=$2`;
        let r = await c.service.psql.query(sqltext,[userId,c.param.noteId]);
        console.log(r.rows);
        c.send(JSON.stringify(r.rows));
    }
    async post(c) { //提交/更新笔记
        let isUpdate = c.headers.update;
        let userId = parseInt(c.box.user.userId);
        console.log('isUpdate：',isUpdate);
        console.log("typeof isUpdate：",typeof isUpdate)
        if (isUpdate == 'true') { //更新笔记
            try {
                console.log("标志1");
                let data = JSON.parse(c.body);
                console.log("data：",data)
                console.log("标志2");
                console.log(parseInt(data.noteId));
                let sqlUpdateNote = `UPDATE note SET "noteTitle"=$1,"noteContent"=$2,"userId"=$3 WHERE "noteId"=$4`;
                let r = await c.service.psql.query(sqlUpdateNote,[data.noteTitle,data.noteContent,userId,parseInt(data.noteId)])
                console.log("r：",r)
                if (r.rowCount != 0) { //对是否更新成功做判断
                    c.send('Update note success!')
                } else {
                    c.status(500).send('Update note failed!')
                }
            } catch (error) {
                c.status(500).send("Database's operations failed!")
            }
        }
        else{ // 添加笔记
            let userId = c.box.user.userId;
            try {
                let data = JSON.parse(c.body);
                let sqlCreateNote = `INSERT INTO `
                                    +`note("userId","noteTitle","noteContent")`
                                    +`VALUES ($1,$2,$3)`;
                let r = await c.service.psql.query(sqlCreateNote,[userId,data.noteTitle,data.noteContent])
                if (r.rowCount != 0) { //对是否提交成功做判断
                    c.send('Post note success!')
                } else {
                    c.status(500).send('Post note failed!')
                }
            } catch (error) {
                c.status(500).send("Database's operations failed!")
            }
        }
    }
    async delete(c) { // 删除笔记
        
        let noteId = c.param.noteId;
        try {
            let sqltext = `DELETE FROM note WHERE "noteId"=$1`; // 由于note表中noteId为主键于是只需要去搜索noteId匹配的即可
            let r = await c.service.psql.query(sqltext,[noteId])
            if (r.rowCount != 0) { //对是否删除成功做判断
                c.send('Delete note success!')
            } else {
                c.status(500).send('Delete note failed!')
            }
        } catch (error) {
            c.status(500).send("Database's operations failed!")
        }
    }
}
module.exports=note;